//
//  WWVisitorView.swift
//  SinWEiBo
//
//  Created by 胡良志 on 2018/3/16.
//  Copyright © 2018年 HuLiangZhi. All rights reserved.
//

import UIKit
import SnapKit

protocol WWVisitorViewDelegate : NSObjectProtocol {
    func userWillLogin() //登陆的代理方法
    func userWillRegister() //注册的代理方法
}

class WWVisitorView: UIView {

    //MARK: 代理属性
    weak var delegate : WWVisitorViewDelegate?
    
    
    //MARK: 外界设置界面信息的方法
    open func updateInfo(title: String, imageName: String,isHome: Bool = false) {
        //修改文字
        self.tipLabel.text = title
        //修改圈圈的图片
        self.circleView.image = UIImage(named: imageName)
        //如果在Home则开始动画
        if isHome {
            //执行动画
            startAnimation()
        } else {
            //不是首页
            //隐藏大房子
            self.houseView.isHidden = true
            //隐藏遮罩
            self.backView.isHidden = true
        }
    }
    
    override init(frame: CGRect) {
        super .init(frame: frame)
                setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    //MARK: UI界面
    private func setupUI (){
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(houseView)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        //登陆注册
        self.loginButton.addTarget(self, action: #selector(loginClick), for:.touchUpInside)
        self.registerButton.addTarget(self, action: #selector(registerClick), for: .touchUpInside)
        //约束
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-80)
        }
        houseView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        tipLabel.snp.makeConstraints { (make ) in
            make.centerX.equalTo(circleView)
            make.top.equalTo(circleView.snp.bottom).offset(20)
        }
        backView.snp.makeConstraints { (make ) in
            make.center.equalTo(circleView)
        }
        loginButton.snp.makeConstraints { (make ) in
            make.leading.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        registerButton.snp.makeConstraints { (make ) in
            make.trailing.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        self.backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
    }
    //MARK: 登陆点击方法
    @objc private func loginClick() {
        self.delegate?.userWillLogin()
    }
    //MARK: 注册点击方法
    @objc private func registerClick() {
        self.delegate?.userWillRegister()
    }
    
    //MARK: 懒加载控件
    //圈圈
    private lazy var circleView : UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //大房子
    private lazy var houseView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    //遮罩
    private lazy var backView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    //提示文案
    fileprivate lazy var tipLabel:UILabel = {
        let le = UILabel(title: "关注一些人,回到这里看看有什么惊喜           ", fountSize: 14, textColor: UIColor.darkGray)
        le.numberOfLines = 0
        le.textAlignment = .center
        //设置文字换行,不要使用设置左右边距来限制宽度,不要直接设置约束宽度
        //设置左右: 在计算非常精确的文本自动换行的时候 会导致高度计算的不准确
        le.preferredMaxLayoutWidth = 220
        return le
    }()

    //登陆
    lazy var loginButton:UIButton = {
        let btn = UIButton(title: "登陆", fountSize: 14, textColor: UIColor.darkGray)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    //注册
    lazy var registerButton:UIButton = {
        let btn = UIButton(title: "注册", fountSize: 14, textColor: UIColor.orange)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
    //动画执行的方法˚
   private func startAnimation (){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.toValue = Double.pi * 2 //Double.pi = M_PI
        animation.duration = 20
        //isRemovedOnCompletion: 当动画效果啊完成之后就从图层移除,设置为false 就不会被移除图层, 默认值值是true
        animation.isRemovedOnCompletion = false
        //将动画添加到circleView的图层上
        circleView.layer.add(animation, forKey: nil)
    }
    
}
